//
//  InspirationVC.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/9.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class InspirationVC: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout {

    let urlString = "http://q.chanyouji.com/api/v1/search/featured.json"
    var flowLayout = UICollectionViewFlowLayout()
    var collectionview:UICollectionView?
    
    var dataArray = Array<ZLQInspirationModel>()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        self.navigationItem.title = "灵感"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(image: UIImage(named: "HTHome_Menu"), style: UIBarButtonItemStyle.Plain, target: self, action: Selector("showSideView"))
        
        addViews()
        dataAnalysis()
    }
    
    func dataAnalysis() {
        
        let url = NSURL(string: urlString)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
//            print(data)
            do {
                let dic:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
//                print(dic)
                let Dict:NSDictionary = dic["data"] as! NSDictionary
                for dict in (Dict["destinations"] as! NSArray) {
                let model = ZLQInspirationModel()
                    model.setValuesForKeysWithDictionary(dict as! [String : AnyObject])
//                    print("=====\(model.name)")
                    self.dataArray.append(model)
                }
            } catch {
            
            }
            dispatch_async(dispatch_get_main_queue()) { () -> Void in
                self.collectionview?.reloadData()
            }
        }
        
        
    
        task.resume()
    }
    
    func addViews() {
    
        flowLayout.sectionInset = UIEdgeInsetsMake(10,  self.view.frame.size.width*0.04, 5, self.view.frame.size.width*0.04)
        flowLayout.itemSize = CGSize(width: self.view.frame.size.width*0.28, height:self.view.frame.size.height*0.25)
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 10
        collectionview = UICollectionView(frame: CGRectMake(0, 0, self.view.frame.size.width,self.view.frame.size.height), collectionViewLayout: flowLayout)
        collectionview?.delegate = self
        collectionview?.dataSource = self
        collectionview?.backgroundColor = UIColor.whiteColor()
        self.view.addSubview(collectionview!)
        collectionview?.registerClass(ZLQInspirationCell.classForCoder(), forCellWithReuseIdentifier: "cell")

       
    
    }
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as? ZLQInspirationCell
        let model:ZLQInspirationModel = self.dataArray[indexPath.row]
//        print(model.name!)
        cell?.areaName?.text = model.name!
        cell?.englishName?.text = model.name_en!
        cell?.infoImage?.sd_setImageWithURL(NSURL(string: model.photo_url!))
        
        
        return cell!
    }
    
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
        
        let a = ZLQFirstPageVC()
        let model:ZLQInspirationModel = self.dataArray[indexPath.row]
        a.str = model.idInt!
        a.string = model.name!

        self.navigationController?.pushViewController(a, animated: true)
    }

    
    
    func showSideView()->Void {
        
        self.sideMenuViewController.presentLeftMenuViewController()
    }
    
   
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}





